var form, $,areaData;
layui.config({
    base : "../../js/"
})
layui.use(['form','layer','upload','laydate'],function(){
    form = layui.form;
    $ = layui.jquery;
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        laydate = layui.laydate,
        address = layui.address;
    
    //上传头像
    upload.render({
        elem: '.userFaceBtn',
        url: '../../addUserTxImg',
        method : "POST",  
        done: function(res, index, upload){
        	if(res == null){
        		layer.msg("头像更换失败！");
        	}else {
        		layer.msg("头像更换成功，重新登录后生效！");
        		$('#userFace').attr('src',res.xdurl);
        		$("#YuserTximg").attr("src",window.sessionStorage.getItem('userFace'));
        	    $("#ZuserTximg").attr("src",window.sessionStorage.getItem('userFace'));
        		 window.sessionStorage.setItem('userFace',res.xdurl);
			}
        	form.render();
        }
    });
    $.get('../../getLogUser',function(data){
    	globalUserData = data;
    	$("#userid").val(data.id);
        $("#loginName").val(data.loginName);
        $("#username").val(data.username);
        $("#showName").val(data.showName);
        $("#userFace").attr('src',data.HeadImage);
        if(data.state == -1){
        	$("#role").val("普通用户");
        }else{
        	$("#role").val(data.rolename);
        }
        $("#mobile").val(data.mobile);
        $("#Email").val(data.Email);
        $("#areaname").val(data.areaname);
        $("#Introduction").val(data.Introduction);
        if(data.roleid == '1,'){
        	getRoleSj();
        }else {
        	 $("#rolekz").hide();
		}
        form.render();
    })
    
    function getRoleSj(){
    	$.get("../../getRole", function (data) {
  		  $("#sjrole").empty();
  		  html = '';
  		  $.each(data, function(i,val) {  
  			  if(val.id != 1){
  				  html += "<input type='checkbox' value='"+val.id+"' name='roleid' val title='"+val.name+"'>";
  			  }
  			}); 
  		  $("#sjrole").html(html);
  		  form.render();
  	  });
    }
    
    //提交个人资料
    form.on("submit(changeUser)",function(data){
        var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
        	var usxx = data.field;
        	var roleids="";
        	$("input:checkbox[name='roleid']:checked").each(function() { 
        		  roleids = roleids + $(this).val()+",";
        	});
        	if(roleids != ""){
        		layer.confirm("您确认要进行角色变更吗？变更后审核通过生效！", { title: "确认变更" }, function (index) {  
                    layer.close(index); 
                    usxx.roleid = roleids;
                    $.post("../../updateUser",usxx, function(data) {
                		if(data == 0){
                			layer.close(index);
                            layer.msg("角色表更已提交，请耐心等待审核！");
                		}else {
                			layer.close(index);
                            layer.msg("提交失败！");
        				}
                	});
                	layer.close(index);
                });   
        	}else {
        		$.post("../../updateUser",usxx, function(data) {
            		if(data == 0){
            			layer.close(index);
                        layer.msg("提交成功！");
            		}else {
            			layer.close(index);
                        layer.msg("提交失败！");
    				}
            	});
            	layer.close(index);
			}
        },500);
        form.render();
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })
    
    //修改密码
    form.on("submit(changePwd)",function(data){
        var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
            layer.close(index);
            layer.msg("密码修改成功！");
            $(".pwd").val('');
        },2000);
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })
    
})